package com.crack.nga2.activity.userCenter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.crack.nga2.R;
import com.crack.nga2.activity.NgaApplication;
import com.crack.nga2.activity.PostActivity;
import com.crack.nga2.utils.GlobalConst;
import com.crack.nga2.utils.HttpParams;
import com.crack.nga2.volley.PostRequestStep1;

import java.util.Map;

public class PostStep1Activity extends AppCompatActivity {
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_step1);
        mIntent = new Intent(PostStep1Activity.this, PostActivity.class);
        Intent intent = getIntent();
        int pid = intent.getIntExtra("pid", 0);
        int fid = intent.getIntExtra("fid", 0);
        int tid = intent.getIntExtra("tid", 0);
        int comment = intent.getIntExtra("comment", 0);
        String action = intent.getStringExtra("action");

        final HttpParams params = new HttpParams();
        params.put("lite","js");
        params.put("action", action);
        mIntent.putExtra("lite","js");
        mIntent.putExtra("action",action);
        if (pid != 0) {
            params.put("pid", String.valueOf(pid));
            mIntent.putExtra("pid", pid);
        }
        if (fid != 0) {
            params.put("fid", String.valueOf(fid));
            mIntent.putExtra("fid", fid);
        }
        if (tid != 0) {
            params.put("tid", String.valueOf(tid));
            mIntent.putExtra("tid", tid);
        }
        if (comment != 0) {
            params.put("comment", String.valueOf(comment));
            mIntent.putExtra("comment", comment);
        }
        String url = GlobalConst.BASE_URL + "post.php?" + params.toString();
        RequestQueue mQueue = NgaApplication.getQueue();
        mQueue.add(new PostRequestStep1(Request.Method.GET, url,
                new Response.Listener<Map<String, String>>() {
                    @Override
                    public void onResponse(Map<String, String> response) {
                        Log.i("step1", response.toString());
                        for (Map.Entry<String, String> entry : response.entrySet()) {
                            //添加content、auth、attach_url
                            mIntent.putExtra(entry.getKey(), entry.getValue());
                        }
                        startActivity(mIntent);
                        finish();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i("error","..."+error.getMessage());
                        finish();
                    }
                }));
    }
}
